package ch.bfh.abcmmh.view.controlview.history.subpanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JPanel;

import ch.bfh.abcmmh.controller.operating.history.HistoryWriter;
import ch.bfh.abcmmh.view.GUIController;

public class HistoryTab extends JPanel {

	private static final long serialVersionUID = 1L;

	private ArrayList<Integer[]> arr;

	public HistoryTab(GUIController guiController) {
		Dimension size = new Dimension(HistoryWriter.RESULTLENGTH,
				HistoryWriter.HISTORYMEMORY);
		this.setPreferredSize(size);
		this.setSize(size);
		this.setBackground(Color.LIGHT_GRAY);

	}

	public void updateTab(ArrayList<Integer[]> hist) {
		arr = hist;
		repaint();
	}

	public ArrayList<Integer[]> getHistoryArray() {
		return arr;
	}

	private Color getHistoryColor(int i) {

		switch (i) {
		case 1:
			return Color.BLUE;
		case 2:
			return  Color.DARK_GRAY;
		case 3:
			return Color.RED;
		case 4:
			return Color.BLACK;
		default:
			return Color.BLACK;
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (arr != null) {
			for (int i = 0; i < arr.size(); i++) {
				for (int j = 0; j < arr.get(i).length; j++) {
					if (arr.get(i)[j] > 0) {
						g.setColor(getHistoryColor(arr.get(i)[j]));
					} else {
						g.setColor(Color.LIGHT_GRAY);
					}
					g.drawRect(j, i, 1, 1);
				}
			}
		}
	}

	public BufferedImage getSnapShot() {
		int w = HistoryWriter.RESULTLENGTH;
		int h = HistoryWriter.HISTORYMEMORY;

		BufferedImage image = new BufferedImage(w, h,
				BufferedImage.TYPE_INT_ARGB);

		this.getTreeLock();
		this.paint(image.createGraphics());

		return image;

	}

}
